package com.oocl.chat.webcontroller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.oocl.chat.controller.ChatServer;
import com.oocl.chat.util.MapOperate;

public class Sender {
	public static String launchTime="0";
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private Dom dom;
	private ChatServer chatServer;
	public Sender() {
		try {
			dom = new Dom();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public void send(String addr, OutputStream out) throws IOException {
		
		List<String> userlist=MapOperate.getAllUserList();
		switch (addr) {

			case "/openServer":
				System.out.println("!!!");
				chatServer = new ChatServer();
				chatServer.start();
				launchTime = sdf.format(new Date());
				dom.writeHome(out, userlist);
				break;
			case "/closeServer":
				launchTime="0";
				chatServer.shutdown();
				dom.writeHome(out, null);
				System.out.println("关闭");
				break;
			case "/":
				dom.writeHome(out, userlist);
				break;
			default:
				dom.writeNotFoundPage(out);
				System.out.println("not found");
				break;
			}
		}
}
